<?php
	function declinePageIfLoggedIn()
	{
		$currentUser = new user();
		if($currentUser->isLoggedIn())
		{
			$previousPage = "index.php";
			$prospectPreviousPage = getPreviousPage();
			if(!empty($prospectPreviousPage))
			{
				if(isset($_COOKIE['emailAddress']))
				{
					if($_COOKIE['emailAddress']==$currentUser->emailAddress)
					{
						$previousPage = $prospectPreviousPage;
					}
				}
			}
			header("Location: ".$previousPage);
			exit();
		}
	}
	
	function declinePageIfNotLoggedIn()
	{	
		$currentUser = new user();
		
		if(!($currentUser->isLoggedIn()))
		{
			$previousPage = "index.php";
			$prospectPreviousPage = getPreviousPage();
			
			if(!empty($prospectPreviousPage))
			{
				if(isset($_COOKIE['emailAddress']))
				{
					if($_COOKIE['emailAddress']==$currentUser->emailAddress)
					{
						$previousPage = $prospectPreviousPage;
					}
				}
			}
			header("Location: ".$previousPage);
			exit();
		}
	}
		
	function declinePageIfNotAdmin()
	{
		$currentUser = new user();
		
		if($currentUser->isLoggedIn())
		{
			if($currentUser->type != 'admin')
			{
				$previousPage = "index.php";
				$prospectPreviousPage = getPreviousPage();
				if(!empty($prospectPreviousPage))
				{
					$previousPage = $prospectPreviousPage;
				}
				header("Location: ".$previousPage);
				exit();
			}
		}		
	}
	
	function declinePageIfAdmin()
	{
		$currentUser = new user();
		
		if($currentUser->isLoggedIn())
		{
			if($currentUser->type == 'admin')
			{
				$previousPage = "index.php";
				$prospectPreviousPage = getPreviousPage();
				if(!empty($prospectPreviousPage))
				{
					$previousPage = $prospectPreviousPage;
				}
				header("Location: ".$previousPage);
				exit();
			}
		}		
	}
	
	function getPreviousPage()
	{
		if(isset($_SESSION['previousPage']))
		{
			return $_SESSION['previousPage'];
		}
		else
		{
			return false;
		}
	}
	
	function getCurrentPage()
	{
		$currentFile = $_SERVER["SCRIPT_NAME"];
		$parts = Explode('/', $currentFile);
		$currentFile = $parts[count($parts) - 1];
		if(empty($_SERVER["QUERY_STRING"]))
		{
			return $currentFile;
		}
		else
		{
			return $currentFile."?".$_SERVER["QUERY_STRING"];
		}
	}		
	
	function setPreviousPage()
	{
		if(isset($_SESSION['currentPage']))
		{
			$previousFile = $_SESSION['currentPage'];
			$currentFile = getCurrentPage();
		
			if($previousFile != $currentFile)
			{
				$_SESSION['previousPage']= $previousFile;
				return true;
			}
		}
		else
		{
			$_SESSION['previousPage']= "index.php";
		}
		return false;
	}
	
	function setCurrentPage()
	{
		$currentFile = getCurrentPage();
		if(isset($_SESSION['currentPage']))
		{
			if($_SESSION['currentPage'] != $currentFile)
			{
				$_SESSION['currentPage']= $currentFile;
			}
		}
		else
		{
			$_SESSION['currentPage'] = $currentFile;
		}
	}
	
	function getCurrentFile()
	{
		$currentFile = $_SERVER["SCRIPT_NAME"];
		$parts = Explode('/', $currentFile);
		$currentFile = $parts[count($parts) - 1];
		return $currentFile;
	}
?>